/**
 * @description The method will send a Ajax request with Promise.
 * @param {String} url 
 * @returns {Promise}
 */

function get(url) {

    return new Promise((resolve, reject) => {
        let req = new XMLHttpRequest()

        req.open('GET', url)
        req.onload = () => {
            if (req.status === 200) {
                resolve(req.response)
            } else {
                reject(Error(req.response))
            }
        }
        req.onerror = _ => reject(Error(req.response))
        req.send()

    })
}

//  send one Ajax request.
get('http://gank.io/api/search/query/listview/category/Android/count/10/page/1')
    .then(data => console.log(data))
    .catch(error => console.log(error))

//  send three Ajax requests at the same time, in the end console.log 'end'.
Promise.all([get('http://gank.io/api/search/query/listview/category/Android/count/10/page/1')
    .then(data => console.log(data)), get('http://gank.io/api/search/query/listview/category/Android/count/10/page/2')
    .then(data => console.log(data)), get('http://gank.io/api/search/query/listview/category/Android/count/10/page/3')
    .then(data => console.log(data))
]).then(_ => console.log('end'))

function post(url, data) {
    return new Promise((resolve, reject) => {
        let req = new XMLHttpRequest()
        req.open('POST', url)
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
        req.send(data)
        req.onload = () => {
            if (req.status === 200) {
                resolve(req.response)
            } else {
                reject(Error(req.response))
            }
        }
        req.onerror = _ => reject(Error(req.response))
    })
}